﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using System.Runtime.Serialization.Formatters.Binary;


namespace Qiang.Data
{
    public static class FileOperation
    {

        public static bool SaveToBinary(string filename, object obj)
        {
            if (filename == "" || filename == null)
            {
                return true;
            }
            else
            {
                BinaryFormatter bf = new BinaryFormatter();

                Stream stream = new FileStream(filename, FileMode.Create, FileAccess.Write);

                try
                {
                    bf.Serialize(stream, obj);
                }
                catch (Exception ex)
                {
                    return false; 
                }
                finally
                {
                    stream.Close(); 
                }

                return true;
            }
        }

        public static object OpenFromBinary(string fname, ref  bool b)
        {


            BinaryFormatter bf = new BinaryFormatter();

            //将文件属性设为普通

            Stream stream = new FileStream(fname, FileMode.Open, FileAccess.Read, FileShare.None);

            object obj;

            try
            {
                obj = bf.Deserialize(stream);
            }
            catch (Exception ex)
            {
                b = false;
                return null;
            }
            finally
            {
                stream.Close();
            }

            b = true;
            return obj;


        }

        /// <summary>
        /// 将文本文件写入到硬盘
        /// </summary>
        /// <param name="pfname"></param>
        /// <returns></returns>
        public static bool WriteFile(string pfname, string content)
        {
            FileInfo MyFile = new FileInfo(pfname);
            if (MyFile.Exists == true)
            {
                try
                {
                    MyFile.Attributes = FileAttributes.Normal;
                    MyFile.Delete();
                    FileStream FS = MyFile.Create();
                    FS.Close();
                    MyFile.Attributes = FileAttributes.Normal;
                    StreamWriter Sw = MyFile.AppendText();

                    Sw.Write(content, Encoding.GetEncoding("GB2312"), false);
                    Sw.Flush();
                    Sw.Close();
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }

            }
            else
            {
                try
                {
                    FileStream FS = MyFile.Create();

                    FS.Close();
                    MyFile.Attributes = FileAttributes.Normal;
                    StreamWriter Sw = MyFile.AppendText();
                    Sw.Write(content, Encoding.GetEncoding("GB2312"), false);
                    Sw.Flush();
                    Sw.Close();
                }
                catch (Exception ex)
                {
                    //throw new ExecutionEngineException(ex.Message);
                    return false;
                }


            }

            return true;
        }

    }
}

